.TH std::conditional 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::conditional \- std::conditional

.SH Synopsis
   Defined in header <type_traits>
   template< bool B, class T, class F >  \fI(since C++11)\fP
   struct conditional;

   Provides member typedef type, which is defined as T if B is true at compile time, or
   as F if B is false.

   If the program adds specializations for std::conditional, the behavior is undefined.

.SH Member types

   Member type Definition
   type        T if B == true, F if B == false

.SH Helper types

   template< bool B, class T, class F >                      \fI(since C++14)\fP
   using conditional_t = typename conditional<B,T,F>::type;

.SH Possible implementation

   template<bool B, class T, class F>
   struct conditional { using type = T; };

   template<class T, class F>
   struct conditional<false, T, F> { using type = F; };

.SH Example


// Run this code

 #include <iostream>
 #include <type_traits>
 #include <typeinfo>

 int main()
 {
     using Type1 = std::conditional<true, int, double>::type;
     using Type2 = std::conditional<false, int, double>::type;
     using Type3 = std::conditional<sizeof(int) >= sizeof(double), int, double>::type;

     std::cout << typeid(Type1).name() << '\\n';
     std::cout << typeid(Type2).name() << '\\n';
     std::cout << typeid(Type3).name() << '\\n';
 }

.SH Possible output:

 int
 double
 double

.SH See also

   enable_if conditionally removes a function overload or template specialization from
   \fI(C++11)\fP   overload resolution
             \fI(class template)\fP
